﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using ESOL.Common.Entity;
using ESOL.Business.Proj;
using ESOL.WinUI.MasterData;
using ESOL.Common;
using ESOL.WinUI.Bpt;

namespace ESOL.WinUI.Proj
{
    public partial class frmMaintainMBOMMaterial : BaseForm.frmBaseDialogyMaintainForm
    {
        #region vars
        private MstProductEntity mSelectedProduct;
        private DataRow drSelectedBomMaterial;
        #endregion

        #region ctors
        public frmMaintainMBOMMaterial(MstProductEntity m)
        {
            InitializeComponent();
            this.mSelectedProduct = m;
        }
        #endregion

        #region functions
        private void BindCombobox()
        {
            UICommon.UICommonFunctions.BindComboboxByCommonItem(cmbManufacturingType, "PICKT", true);
        }

        private void Init()
        {
            if (mSelectedProduct != null)
            {
                txtProduct.Tag = mSelectedProduct;
                txtProduct.Text = mSelectedProduct.ProductName;
                txtProductCode.Text = mSelectedProduct.ProductCode;
                txtQueryProduct.Text = mSelectedProduct.ProductName;
                txtQueryProductCode.Text = mSelectedProduct.ProductCode;

                btnApply.Enabled = false;
            }
        }

        private void BindGridData()
        {
            DataTable dt = ProjMBOMMaterialBs.GetDtAllByProductID(mSelectedProduct.ProductId);
            gvMain.DataSource = dt;
        }

        private string GetCondition()
        {
            string sCondition = "";
            if (string.IsNullOrEmpty(txtQueryProduct.Text) == false)
            {
                sCondition += "and mp.PRODUCT_NAME like '%" + txtQueryProduct.Text + "%'";
            }
            if (string.IsNullOrEmpty(txtQueryProductCode.Text) == false)
            {
                sCondition += "and mp.PRODUCT_CODE like '%" + txtQueryProductCode.Text + "%'";
            }
            return sCondition;
        }

        private void BindGridDataByQuery()
        {
            DataTable dt = ProjMBOMMaterialBs.GetDtAllByFuzzyCondition(GetCondition());
            gvMain.DataSource = dt;
        }

        private bool CheckData()
        {
            DataTable dt = gvMain.DataSource as DataTable;
            foreach (DataRow dr in dt.Rows)
            {
                if (string.IsNullOrEmpty(dr["MANUFACTURING_TYPE"].ToString()))
                {
                    MessageBox.Show("加工类型不能为空！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return false;
                }
                if (string.IsNullOrEmpty(dr["PICKING_MODEL"].ToString()))
                {
                    MessageBox.Show("放料规格不能为空！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return false;
                }
                if (string.IsNullOrEmpty(dr["UNIT_WEIGHT"].ToString()))
                {
                    MessageBox.Show("单位总量不能为空！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return false;
                }
            }
            return true;
        }

        #endregion

        #region events
        protected override void OnShown(EventArgs e)
        {
            base.OnShown(e);
            BindCombobox();
            Init();
            BindGridData();
        }

        //private void gridView1_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        //{
        //    drSelectedBomMaterial = gridView1.GetDataRow(e.FocusedRowHandle);
        //    if (drSelectedBomMaterial != null)
        //    {
        //        txtMaterialName.Text = drSelectedBomMaterial["MATERIAL_NAME"].ToString();
        //        txtNetSize.Text = drSelectedBomMaterial["NET_SIZE"].ToString();
        //        txtGrossSize.Text = drSelectedBomMaterial["GROSS_SIZE"].ToString();
        //        txtRemark.Text = drSelectedBomMaterial["REMARK"].ToString();
        //        txtPickingModel.Text = drSelectedBomMaterial["PICKING_MODEL"].ToString();
        //        txtUnitWeight.Text = drSelectedBomMaterial["UNIT_WEIGHT"].ToString();
        //        cmbManufacturingType.SetSelectedValue(drSelectedBomMaterial["MANUFACTURING_TYPE"].ToString());
        //    }
        //}
        private void gridView1_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            if (gridView1.GetSelectedRows().Length != 0)
            {
                drSelectedBomMaterial = gridView1.GetDataRow(gridView1.GetSelectedRows()[0]);
                if (drSelectedBomMaterial != null)
                {
                    txtMaterialName.Text = drSelectedBomMaterial["MATERIAL_NAME"].ToString();
                    txtNetSize.Text = drSelectedBomMaterial["NET_SIZE"].ToString();
                    txtGrossSize.Text = drSelectedBomMaterial["GROSS_SIZE"].ToString();
                    txtRemark.Text = drSelectedBomMaterial["REMARK"].ToString();
                    txtPickingModel.Text = drSelectedBomMaterial["PICKING_MODEL"].ToString();
                    txtUnitWeight.Text = drSelectedBomMaterial["UNIT_WEIGHT"].ToString();
                    cmbManufacturingType.SetSelectedValue(drSelectedBomMaterial["MANUFACTURING_TYPE"].ToString());
                }
            }
        }

        private void tsmNewLine_Click(object sender, EventArgs e)
        {
            DataTable dt = (DataTable)gvMain.DataSource;
            DataRow dr = dt.NewRow();
            dt.Rows.Add(dr);
            drSelectedBomMaterial = dr;
        }

        private void tsmDelLine_Click(object sender, EventArgs e)
        {
            if (gridView1.GetSelectedRows().Length != 0)
            {
                drSelectedBomMaterial = gridView1.GetDataRow(gridView1.GetSelectedRows()[0]);
                if (drSelectedBomMaterial != null)
                {
                    DataTable dt = (DataTable)gvMain.DataSource;
                    dt.Rows.Remove(drSelectedBomMaterial);
                }
            }
        }

        private void repositoryItemButtonEdit1_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            frmMaintainProductQuery frm = new frmMaintainProductQuery();
            //frm.sCondition = " and C.PRODUCT_TYPE_CODE='D' ";
            if (frm.ShowDialog() == DialogResult.OK && frm.SelectedProduct != null)
            {
                drSelectedBomMaterial["BOM_MATERIAL_ID"] = Guid.NewGuid().ToString();
                drSelectedBomMaterial["PRODUCT_ID"] = mSelectedProduct.ProductId;
                drSelectedBomMaterial["MATERIAL_ID"] = frm.SelectedProduct.ProductId;
                drSelectedBomMaterial["MATERIAL_NAME"] = frm.SelectedProduct.ProductName;
                drSelectedBomMaterial["MATERIAL_CODE"] = frm.SelectedProduct.ProductCode;
            }
        }

        private void cmbManufacturingType_SelectionChangeCommitted(object sender, EventArgs e)
        {
            if (cmbManufacturingType.SelectedValue != null)
            {
                if (string.IsNullOrEmpty(cmbManufacturingType.SelectedValue.ToString()) == false)
                {
                    drSelectedBomMaterial["MANUFACTURING_TYPE"] = cmbManufacturingType.SelectedValue.ToString();
                }
            }
        }

        private void txtPickingModel_EditValueChanged(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtPickingModel.Text))
            {
                MessageBox.Show("放料规格不能为空！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            drSelectedBomMaterial["PICKING_MODEL"] = txtPickingModel.Text;
        }

        private void txtNetSize_EditValueChanged(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtNetSize.Text))
            {
                MessageBox.Show("净尺寸不能为空！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            drSelectedBomMaterial["NET_SIZE"] = txtNetSize.Text;
        }

        private void txtGrossSize_EditValueChanged(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtGrossSize.Text))
            {
                MessageBox.Show("毛尺寸不能为空！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            drSelectedBomMaterial["GROSS_SIZE"] = txtGrossSize.Text;
        }

        private void txtUnitWeight_EditValueChanged(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtUnitWeight.Text))
                return;
            drSelectedBomMaterial["UNIT_WEIGHT"] = txtUnitWeight.Text;
        }

        private void txtRemark_EditValueChanged(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtRemark.Text))
                return;
            drSelectedBomMaterial["REMARK"] = txtRemark.Text;
        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            BindGridDataByQuery();
            DataTable dt = (DataTable)gvMain.DataSource;
            if (dt != null && dt.Rows.Count > 0)
            {
                btnApply.Enabled = true;
            }
        }

        private void btnApply_Click(object sender, EventArgs e)
        {
            if (mSelectedProduct == null)
            {
                MessageBox.Show("套用失败！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            DataTable dt = (DataTable)gvMain.DataSource;
            foreach (DataRow dr in dt.Rows)
            {
                dr["BOM_MATERIAL_ID"] = Guid.NewGuid().ToString();
                dr["PRODUCT_ID"] = mSelectedProduct.ProductId;
            }
            MessageBox.Show("套用成功！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        protected override void Click_Save()
        {
            if (CheckData() == false)
                return;
            DataTable dt = (DataTable)gvMain.DataSource;
            if (dt != null && dt.Rows.Count > 0)
            {
                if (ProjMBOMMaterialBs.Save(mSelectedProduct, dt))
                {
                    MessageBox.Show("保存成功！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    DialogResult = DialogResult.OK;
                }
                else
                {
                    MessageBox.Show("保存失败！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
            }
            else
            {
                MessageBox.Show("原材料不能为空！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
        }
        #endregion

    }
}
